/*
 * @(#)ListExplorer.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */
package grupb.upf.gui;

import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;

/**
 * <p>This is the List of files.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public class ListExplorer extends javax.swing.JPanel {

    /**
     * The list model of the list of files.
     */
    private DefaultListModel mdlFiles;
    /**
     * The type of folders (image [0] or video [1] folders).
     */
    private int type;
    /**
     * Tree icons size by default
     */
    private static final int DEFAULT_LIST_ICON_SIZE = 140;
    /**
     * Tree icon size
     */
    private int iconSizeList = DEFAULT_LIST_ICON_SIZE;
    /**
     * The initial of the tree.
     */
    private P2Piki p2piki;
    /**
     * The type of photos format accepted.
     */
    private static final String [] PHOTO_FORMATS =
    {"jpg", "jpeg", "gif", "png"};
    /**
     * The type of videos format accepted.
     */
    private static final String [] VIDEO_FORMATS =
    {"avi", "mpeg", "mpg", "asf", "wmv", "wma", "ogg", "wav", "flv"};

    /** Creates new form ListExplorer */
    public ListExplorer(P2Piki p2p, final int t) {
        mdlFiles = new DefaultListModel();
        this.type = t;
        initComponents();
        this.p2piki = p2p;
        if (t == 0) {
            openImageButton1.setText("Import a Photo");
            openImageButton1.setIcon(
                    new javax.swing.ImageIcon(getClass()
                    .getResource("/graphics/folder_image_import.png")));
        } else {
            openImageButton1.setText("Import a Video");
            openImageButton1.setIcon(
                    new javax.swing.ImageIcon(getClass()
                    .getResource("/graphics/folder_video_import.png")));
        }
    }

    /**
     * Method to change the default icons size.
     * @param size the new size of the icons
     */
    public void setIconSize(final int size) {
        this.iconSizeList = size;
        filesList.setCellRenderer(new FileRenderer(iconSizeList, type));
    }

    /**
     * Method to remove the elements of the list.
     */
    public void removeAllElements() {
        mdlFiles.removeAllElements();
    }

    /**
     * Method to add an element to the list.
     * @param f the file to add
     */
    public void addElement(final File f) {
        mdlFiles.addElement(f);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        largeIconsButton = new javax.swing.JButton();
        mediumIconsButton = new javax.swing.JButton();
        smallIconsButton = new javax.swing.JButton();
        explorePanel = new javax.swing.JPanel();
        filesScrollPane = new javax.swing.JScrollPane(filesList);
        filesList = new javax.swing.JList(mdlFiles);
        iconSizeSlider = new javax.swing.JSlider();
        viewsLabel = new javax.swing.JLabel();
        openImageButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(542, 546));

        largeIconsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/viewLarge.png"))); // NOI18N
        largeIconsButton.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        largeIconsButton.setFocusPainted(false);
        largeIconsButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/viewLargePressed.png"))); // NOI18N
        largeIconsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                largeIconsButtonActionPerformed(evt);
            }
        });

        mediumIconsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/viewMedium.png"))); // NOI18N
        mediumIconsButton.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        mediumIconsButton.setFocusPainted(false);
        mediumIconsButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/viewMediumPressed.png"))); // NOI18N
        mediumIconsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mediumIconsButtonActionPerformed(evt);
            }
        });

        smallIconsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/viewSmall.png"))); // NOI18N
        smallIconsButton.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        smallIconsButton.setFocusPainted(false);
        smallIconsButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/viewSmallPressed.png"))); // NOI18N
        smallIconsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                smallIconsButtonActionPerformed(evt);
            }
        });

        explorePanel.setBackground(new java.awt.Color(255, 255, 255));
        explorePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));

        filesScrollPane.setBackground(new java.awt.Color(255, 255, 255));
        filesScrollPane.setBorder(null);
        filesScrollPane.setPreferredSize(new java.awt.Dimension(509, 500));

        filesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        filesList.setCellRenderer(new FileRenderer(iconSizeList, type));
        filesList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
        filesList.setVisibleRowCount(-1);
        filesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                filesListMouseClicked(evt);
            }
        });
        filesScrollPane.setViewportView(filesList);

        javax.swing.GroupLayout explorePanelLayout = new javax.swing.GroupLayout(explorePanel);
        explorePanel.setLayout(explorePanelLayout);
        explorePanelLayout.setHorizontalGroup(
            explorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, explorePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(filesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                .addContainerGap())
        );
        explorePanelLayout.setVerticalGroup(
            explorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, explorePanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(filesScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        iconSizeSlider.setBackground(new java.awt.Color(255, 255, 255));
        iconSizeSlider.setMaximum(-50);
        iconSizeSlider.setMinimum(-300);
        iconSizeSlider.setValue(-140);
        iconSizeSlider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                iconSizeSliderMouseDragged(evt);
            }
        });

        viewsLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        viewsLabel.setText("Views");

        openImageButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/folder_image_import.png"))); // NOI18N
        openImageButton1.setText("Import a Photo");
        openImageButton1.setToolTipText("Import image");
        openImageButton1.setBorderPainted(false);
        openImageButton1.setFocusPainted(false);
        openImageButton1.setMaximumSize(new java.awt.Dimension(54, 40));
        openImageButton1.setMinimumSize(new java.awt.Dimension(54, 40));
        openImageButton1.setPreferredSize(new java.awt.Dimension(54, 40));
        openImageButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openImageButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(viewsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(largeIconsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(mediumIconsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(10, 10, 10)
                    .addComponent(smallIconsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(iconSizeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(29, 29, 29)
                    .addComponent(openImageButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(explorePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(openImageButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(iconSizeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(viewsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                            .addComponent(mediumIconsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(largeIconsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(smallIconsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(explorePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void iconSizeSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_iconSizeSliderMouseDragged
        // TODO add your handling code here:
        setIconSize(-iconSizeSlider.getValue());
    }//GEN-LAST:event_iconSizeSliderMouseDragged

    private void largeIconsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_largeIconsButtonActionPerformed
        // TODO add your handling code here:
        setIconSize(300);
        iconSizeSlider.setValue(-300);
}//GEN-LAST:event_largeIconsButtonActionPerformed

    private void mediumIconsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mediumIconsButtonActionPerformed
        // TODO add your handling code here:
        setIconSize(140);
        iconSizeSlider.setValue(-140);
    }//GEN-LAST:event_mediumIconsButtonActionPerformed

    private void smallIconsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_smallIconsButtonActionPerformed
        // TODO add your handling code here:
        setIconSize(60);
        iconSizeSlider.setValue(-60);
    }//GEN-LAST:event_smallIconsButtonActionPerformed

    private void filesListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_filesListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            final int index = filesList.getSelectedIndex();
            final File f = (File) mdlFiles.get(index);
            if (f.isDirectory()) {
                p2piki.directorySelected(type, f);
            } else if (type == 0) {
                boolean isPhoto = false;
                final String path = f.getAbsolutePath();
                //format
                final String extension =
                        path.substring(path.lastIndexOf(".") + 1);
                for (int i = 0; i < PHOTO_FORMATS.length; i++) {
                    if (PHOTO_FORMATS[i].equalsIgnoreCase(extension)) {
                        isPhoto = true;
                    }
                }
                if (isPhoto) {
                    p2piki.viewPhoto((File) mdlFiles.get(index));
                } else {
                    p2piki.addError("It isn't an accepted format.");
                }
            } else if (type == 1) {
                boolean isVideo = false;
                final String path = f.getAbsolutePath();
                //format
                final String extension =
                        path.substring(path.lastIndexOf(".") + 1);
                for (int i = 0; i < VIDEO_FORMATS.length; i++) {
                    if (VIDEO_FORMATS[i].equalsIgnoreCase(extension)) {
                        isVideo = true;
                    }
                }
                if (isVideo) {
                    p2piki.viewVideo((File) mdlFiles.get(index));
                } else {
                    p2piki.addError("It isn't an accepted format.");
                }
            }
        }
    }//GEN-LAST:event_filesListMouseClicked

    private void openImageButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openImageButton1ActionPerformed
        // TODO add your handling code here:
        if (type == 0) {
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
            if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                final String path = fc.getSelectedFile().getAbsolutePath();
                boolean isPhoto = false;
                //format
                final String extension =
                        path.substring(path.lastIndexOf(".") + 1);
                for (int i = 0; i < PHOTO_FORMATS.length; i++) {
                    if (PHOTO_FORMATS[i].equalsIgnoreCase(extension)) {
                        isPhoto = true;
                    }
                }
                if (isPhoto) {
                    p2piki.viewPhoto(fc.getSelectedFile());
                } else {
                    p2piki.addError("It isn't an accepted format.");
                }
            }
        } else if (type == 1) {
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
            if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                final String path = fc.getSelectedFile().getAbsolutePath();
                boolean isVideo = false;
                //format
                final String extension =
                        path.substring(path.lastIndexOf(".") + 1);
                for (int i = 0; i < VIDEO_FORMATS.length; i++) {
                    if (VIDEO_FORMATS[i].equalsIgnoreCase(extension)) {
                        isVideo = true;
                    }
                }
                if (isVideo) {
                    p2piki.viewVideo(fc.getSelectedFile());
                } else {
                    p2piki.addError("It isn't an accepted format.");
                }
            }
        }
}//GEN-LAST:event_openImageButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel explorePanel;
    private javax.swing.JList filesList;
    private javax.swing.JScrollPane filesScrollPane;
    private javax.swing.JSlider iconSizeSlider;
    private javax.swing.JButton largeIconsButton;
    private javax.swing.JButton mediumIconsButton;
    private javax.swing.JButton openImageButton1;
    private javax.swing.JButton smallIconsButton;
    private javax.swing.JLabel viewsLabel;
    // End of variables declaration//GEN-END:variables
}
